package com.yty.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yty.Utils.JwtsUtils;
import com.yty.dao.EmpExprMapper;

import com.yty.dao.EmpMapper;
import com.yty.page.PageResult;
import com.yty.pojo.Emp;
import com.yty.pojo.EmpLog;
import com.yty.param.EmpQueryParam;
import com.yty.pojo.LoginInfo;
import com.yty.service.EmpLogService;
import com.yty.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private EmpExprMapper empExprMapper;

    @Autowired
    private EmpLogService empLogService;

    @Override
    public PageResult findEmpList(EmpQueryParam param) {
        PageHelper.startPage(param.getPage(),param.getPageSize());
        Page<Emp> empList = (Page<Emp>) empMapper.findEmpList(param);
        PageResult pageResult = new PageResult(empList.getTotal(),empList.getResult());
        return pageResult;
    }
    @Override
    public List<Emp> findEmpList1(EmpQueryParam param) {
        List<Emp> empList = empMapper.findEmpList(param);
        return empList;
    }

    /**
     * 新增员工
     * @param emp
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addEmp(Emp emp) {
        try {
            emp.setCreateTime(LocalDateTime.now());
            emp.setUpdateTime(LocalDateTime.now());
            empMapper.addEmp(emp);
            emp.getExprList().forEach(empExpr -> {
                empExpr.setEmpId(emp.getId());
            });
            empExprMapper.addEmpExpr(emp.getExprList());

        }finally {
            //写入操作日志
          empLogService.insertLog(new EmpLog(null,LocalDateTime.now(),"新增员工"+ emp));
        }

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteEmpById(Integer[] ids) {
        int idemp = empMapper.deleteEmpById(ids);
        int idempExpr = empExprMapper.deleteEmpExprById(ids);
        if (idemp>0){
            return 1;
        }
        return 0;
    }

    @Override
    public Emp findEmpById(Integer id) {
        Emp emp = empMapper.findEmpById(id);
        return emp;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateEmp(Emp emp) {
        if (emp.getId()!=null&& emp.getId()>0
        && emp.getUsername()!=null&&!emp.getUsername().equals("")
        && emp.getName()!=null&&!emp.getName().equals("")
        && emp.getPhone()!=null&&!emp.getPhone().equals("")
        && emp.getGender()!=null){
            //1先更新基本信息
            int empRow= empMapper.updateEmp(emp);
            //2在删除员工工作经历的信息
            int empExpRow = empExprMapper.deleteEmpExprById(emp.getId());
            //3在直接添加跟新得工作信息
            emp.getExprList().forEach(empExpr -> {
                empExpr.setEmpId(emp.getId());
            });
            int i = empExprMapper.addEmpExpr(emp.getExprList());
            return 1;
        }
        return 0;
    }

    @Override
    public LoginInfo findEmpByNameAndPassword(Emp emp) {
        if (emp.getUsername()==null||emp.getPassword()==null
                ||emp.getUsername().equals("")||emp.getPassword().equals(""))
            return null;
        Emp emp1 = empMapper.findEmpByNameAndPassword(emp);
        if (emp1==null)return null; //用户名密码错误
        //用户名密码正确
        Map<String,Object> claims = new HashMap<>();
        claims.put("id",emp1.getId());
        claims.put("username",emp1.getUsername());
        String token = JwtsUtils.generateToken(claims);
        return new LoginInfo(emp1.getId(),emp1.getUsername(),emp1.getName(),token);
    }
}
